package com.link.base.wechat.wxtoken.util;

import com.link.base.wechat.basic.util.WxExceptionUtil;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.HttpRequestUtil;
import com.alibaba.fastjson.JSONObject;
import com.link.core.util.StringUtils;

/**
 * 微信Token和ticket接口
 *
 * @author ouyangzhenqiang
 * @date 2019/1/2
 */
public class WxTokenUtil {

    /**
     * 开发者模式获取微信公众号access_token
     * 基于微信公众号应用Id和密钥
     *
     * @author ouyangzhenqiang
     * @date 2019/02/27 16:16
     * @param wxAppId 微信公众号应用Id
     * @param wxAppSecret 微信公众号应用密钥
     */
    public static synchronized String getAccessTokenBySecret(String wxAppId, String wxAppSecret) throws Exception {
        if (StringUtils.isBlank(wxAppId)) {
            throw new ServiceException("WXTOKEN-001");
        }
        if (StringUtils.isBlank(wxAppSecret)) {
            throw new ServiceException("WXTOKEN-004");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        String param = "appid=" + wxAppId + "&secret=" + wxAppSecret + "&grant_type=client_credential";
        String resultStr = HttpRequestUtil.sendGet(url, param);
        JSONObject resultJson = WxExceptionUtil.parse(resultStr);
        return resultJson.getString("access_token");
    }

    /**
     * 第三方平台模式获取微信公众号access_token
     *
     * @author ouyangzhenqiang
     * @date 2019/02/27 16:38
     * @param componentAppId 第三方平台应用Id
     * @param componentAccessToken 第三方平台access_token
     * @param authorizerAppId 授权方公众号应用Id
     * @param authorizerRefreshToken 授权方刷新令牌
     */
    public static synchronized String getAccessTokenByComponent(
        String componentAppId, String componentAccessToken, String authorizerAppId, String authorizerRefreshToken) throws Exception {
        if (StringUtils.isBlank(componentAppId)) {
            throw new ServiceException("WXTOKEN-005");
        }
        if (StringUtils.isBlank(componentAccessToken)) {
            throw new ServiceException("WXTOKEN-006");
        }
        if (StringUtils.isBlank(authorizerAppId)) {
            throw new ServiceException("WXTOKEN-007");
        }
        if (StringUtils.isBlank(authorizerRefreshToken)) {
            throw new ServiceException("WXTOKEN-008");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentAccessToken;
        String param = "{\"component_appid\":\"" + componentAppId + "\",\"authorizer_appid\": \""
            + authorizerAppId + "\" ,\"authorizer_refresh_token\":\"" + authorizerRefreshToken + "\"}";
        String resultStr = HttpRequestUtil.jsonPostThrowException(url, param, null);
        JSONObject resultJson = WxExceptionUtil.parse(resultStr);
        return resultJson.getString("authorizer_access_token");
    }

    /**
     * 根据微信公众号access_token获取JsTicket
     *
     * @author ouyangzhenqiang
     * @date 2019/02/27 16:36
     * @param access_token 微信公众号access_token
     * @param type ticket类型 jsapi/wx_card
     */
    public static synchronized String getJsTicketByAccessToken(String access_token, String type) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXTOKEN-009");
        }
        if (StringUtils.isBlank(type)) {
            type = "jsapi";
        }
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
        String param = "access_token=" + access_token + "&&type=" + type;
        String resultStr = HttpRequestUtil.sendGet(url, param);
        JSONObject resultJson = WxExceptionUtil.parse(resultStr);
        return resultJson.getString("ticket");
    }
}
